//
//  FriendHeader.m
//  FriendsList
//
//  Created by hellovoidworld on 14/12/12.
//  Copyright (c) 2014年 hellovoidworld. All rights reserved.
//

#import "FriendHeader.h"
#import "NomalModel.h"
#import <Masonry.h>
@implementation FriendHeader

+ (instancetype) friendHeaderWithTableView:(UITableView *) tableView {
    static NSString *ID = @"friendHeader";
    FriendHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (nil == header) {
        header = [[self alloc] initWithReuseIdentifier:ID];
    }
    
    return header;
}

/** 重写初始化方法, 给header加上图标、组名 */
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    // 必须先调用super的方法
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        // 1.添加背景按钮
        // 1.1背景
        UIButton *headerButtonView = [[UIButton alloc] init];
        
        // 改变箭头填充方式为居中
        [headerButtonView.imageView setContentMode:UIViewContentModeCenter];
        // 不需要裁剪箭头图片的边界
        [headerButtonView.imageView setClipsToBounds:NO];
        
        [headerButtonView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
        [headerButtonView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        
        // 1.2图标
        // 水平左对齐
        headerButtonView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        // 设置内边距
        headerButtonView.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        // 设置图片
        [headerButtonView setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        
        // 1.3组名
        [headerButtonView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        headerButtonView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        
        // 1.4点击事件
        [headerButtonView addTarget:self action:@selector(headerClicked) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:headerButtonView];
        self.headerButtonView = headerButtonView;
        [self setupTouchView];
        
    }
    return self;
}

/** 子控件布局方法
 在init的时候，只分配的内存，没有初始化控件的尺寸，在此处header已经有了位置尺寸了
 */
- (void)layoutSubviews {
    // 必须先调用父类的方法
    [super layoutSubviews];
    self.headerButtonView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 2);
    //    self.headerButtonView.backgroundColor = [UIColor colorWithRed:119/255.0 green:208/255.0 blue:254/255.0 alpha:1];
    self.headerButtonView.layer.cornerRadius = 5;
    
}

- (void)setupTouchView {
    
    UIView *view = [[UIView alloc]init];
    [self.headerButtonView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.headerButtonView.mas_right).offset(0);
        make.top.mas_equalTo(self.headerButtonView.mas_top).offset(0);
        make.bottom.mas_equalTo(self.headerButtonView.mas_bottom).offset(0);
        make.left.mas_equalTo(self.headerButtonView.titleLabel.mas_right).offset(0);
        
    }];
    view.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchView)];
    [view addGestureRecognizer:tapGesture];
    
    UIImageView *imageV = [[UIImageView alloc]init];
    
    [view addSubview:imageV];
    _imageV = imageV;
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view.mas_centerY);
        make.right.mas_equalTo(view.mas_right).offset(-5);
        make.width.and.height.mas_equalTo(30);
    }];
    imageV.image = [UIImage imageNamed:@"post_yes"];
    imageV.hidden = YES;
}

- (void)touchView{
    _imageV.hidden = !_imageV.hidden;
    self.nomalModel.shouldHidden = _imageV.hidden;
    if (_imageV.hidden) {
        self.headerButtonView.backgroundColor = [UIColor colorWithRed:119/255.0 green:208/255.0 blue:254/255.0 alpha:1];
        self.nomalModel.isSelect = NO;
    }else{
        self.headerButtonView.backgroundColor = [UIColor redColor];
        self.nomalModel.isSelect = YES;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(fireadHeadDidSelect:)]) {
        
        [self.delegate fireadHeadDidSelect:self];
        
    }
}

- (void)setNomalModel:(NomalModel *)nomalModel {
    _nomalModel = nomalModel;
    // 1组名
    NSString *str = _nomalModel.MSG_CATEGORY_TO_NM;
    [self.headerButtonView setTitle:str forState:UIControlStateNormal];
    if (!_nomalModel.isSelect) {
        _nomalModel.shouldHidden = YES;
    } else {
        _nomalModel.shouldHidden = NO;
        NSLog(@"选中");
    
    }

    _imageV.hidden = _nomalModel.shouldHidden;
    if (_imageV.hidden) {
        self.headerButtonView.backgroundColor = [UIColor colorWithRed:119/255.0 green:208/255.0 blue:254/255.0 alpha:1];
    }else{
        self.headerButtonView.backgroundColor = [UIColor magentaColor];
    }
    
}
/** 点击事件 */
- (void) headerClicked {
    // 1.伸展、隐藏组内好友
        self.nomalModel.isOpen = !self.nomalModel.isOpen;
    
    // 2.刷新tableView
    if ([self.delegate respondsToSelector:@selector(friendHeaderDidClickedHeader:)]) {
        [self.delegate friendHeaderDidClickedHeader:self];
    }
}

/**
 被加到父控件之前
 由于tableView刷新数据后，所有header会被重新创建，所以要在这里对箭头朝向做出修改
 */
- (void)didMoveToSuperview {
    // 改变箭头朝向，顺时针旋转90度
    CGFloat rotation = self.nomalModel.isOpen? M_PI_2 : 0;
    self.headerButtonView.imageView.transform = CGAffineTransformMakeRotation(rotation);
}

@end
